//
//  GWTopicCell.m
//  百思不得姐
//
//  Created by qianfeng on 16/3/3.
//  Copyright © 2016年 高炜. All rights reserved.
//

#import "GWTopicCell.h"
#import <UIImageView+WebCache.h>

#import "GWTopicModel.h"
#import "GWPictureView.h"
#import "GWTopicVoiceView.h"
#import "GWTopicVideoView.h"
#import "GWComment.h"
#import "GWUser.h"
@interface GWTopicCell ()
/** 头像*/
@property (weak, nonatomic) IBOutlet UIImageView *headerView;
/** 昵称*/
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
/** 时间*/
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
/** 顶*/
@property (weak, nonatomic) IBOutlet UIButton *dingButton;
/** 踩*/
@property (weak, nonatomic) IBOutlet UIButton *caiButton;
/** 转发*/
@property (weak, nonatomic) IBOutlet UIButton *reportButton;
/** 评论*/
@property (weak, nonatomic) IBOutlet UIButton *commentButton;
@property (weak, nonatomic) IBOutlet UIImageView *sinavView;

@property (weak, nonatomic) IBOutlet UILabel *text_Label;
/** 热门评论*/
@property (weak, nonatomic) IBOutlet UIView *topCommentView;
/** 热门评论内容*/
@property (weak, nonatomic) IBOutlet UILabel *topCommentViewLabel;

@property (weak, nonatomic) GWPictureView *pictureView;
@property (weak, nonatomic) GWTopicVoiceView *voiceView;
@property (weak, nonatomic) GWTopicVideoView *videoView;
@end
@implementation GWTopicCell

+(instancetype)topicCell
{
    return [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:nil options:nil]lastObject];
}
-(GWPictureView *)pictureView
{
    if (_pictureView ==nil) {
        GWPictureView *picture =[GWPictureView pictureView];
        [self.contentView addSubview:picture];
        _pictureView =picture;
    }
    return _pictureView;
}
-(GWTopicVoiceView *)voiceView
{
    if (_voiceView ==nil) {
        GWTopicVoiceView *voice =[GWTopicVoiceView topicVoiceView];
        [self.contentView addSubview:voice];
        _voiceView =voice;
    }
    return _voiceView;
}
-(GWTopicVideoView *)videoView
{
    if (_videoView ==nil) {
        GWTopicVideoView *video =[GWTopicVideoView topicVidoeView];
        [self.contentView addSubview:video];
        _videoView =video;
    }
    return _videoView;
}
- (void)awakeFromNib {
    UIImageView *imageView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mainCellBackground"]];
    self.backgroundView =imageView;
}
-(void)setFrame:(CGRect)frame
{
    frame.origin.x =GWEssencCellMargin;
    frame.size.width -= GWEssencCellMargin*2;
    frame.size.height =self.topic.cellHeight - GWEssencCellMargin;
    frame.origin.y +=GWEssencCellMargin;
    
    [super setFrame:frame];
}
-(void)setTopic:(GWTopicModel *)topic
{
    _topic =topic;
    topic.sina_v =arc4random_uniform(2);
    
    self.sinavView.hidden =!topic.isSina_v;
    [self.headerView showHeaderViewWithIcon:topic.profile_image];
   
    self.nameLabel.text =topic.name;
    self.timeLabel.text =topic.create_time;
    self.text_Label.text =topic.text;
   
    
    [self btn:self.dingButton setCount:topic.ding withPlaceholder:@"顶"];
    [self btn:self.caiButton setCount:topic.cai withPlaceholder:@"踩"];
    [self btn:self.reportButton setCount:topic.repost withPlaceholder:@"分享"];
    [self btn:self.commentButton setCount:topic.comment withPlaceholder:@"评论"];
    
    if (topic.type==GWTopicControllerTypePicture) {
        self.pictureView.hidden =NO;
        self.pictureView.topic =topic;
        self.pictureView.frame =topic.pictureF;
        self.videoView.hidden =YES;
        self.voiceView.hidden =YES;
    }
    else if (topic.type==GWTopicControllerTypeVoice)
    {
        self.voiceView.hidden =NO;
        self.voiceView.topic =topic;
        self.voiceView.frame =topic.voiceF;
        
        self.videoView.hidden =YES;
        self.pictureView.hidden =YES;
    }
    else if (topic.type ==GWTopicControllerTypeVideo)
    {
        self.videoView.hidden =NO;
        self.videoView.topic =topic;
        self.videoView.frame =topic.videoF;
        
        self.voiceView.hidden =YES;
        self.pictureView.hidden =YES;
    }
    else
    {
        self.videoView.hidden =YES;
        self.pictureView.hidden =YES;
        self.voiceView.hidden =YES;
    }
    GWComment *coment =[topic.top_cmt firstObject];
    
    
    if (coment) {
        
        self.topCommentView.hidden =NO;
        self.topCommentViewLabel.text =[NSString stringWithFormat:@"%@ : %@",coment.user.username,coment.content];
    }
    else
    {
        self.topCommentView.hidden =YES;
    }
}
-(void)btn:(UIButton *)btn setCount:(NSInteger )count withPlaceholder:(NSString *)placeholder
{
    if (count>10000) {
        placeholder = [NSString stringWithFormat:@"%.1f 万",count/10000.0];
    }
    else if (count>0)
    {
        placeholder =[NSString stringWithFormat:@"%ld",count];
    }
    [btn setTitle:placeholder forState:UIControlStateNormal];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (IBAction)more {
    
    UIActionSheet *action =[[UIActionSheet alloc]initWithTitle:nil delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"分享",@"举报", nil];
    [action showInView:self.window];
}

@end
